"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.postCrawl = postCrawl;
exports.craw = craw;
exports.getCookie = getCookie;
exports.normalCraw = normalCraw;
var _axios = _interopRequireDefault(require("axios"));
var _https = _interopRequireDefault(require("https"));
var _ = require(".");
function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const baseHeaders = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36',
    Connection: 'keep-alive',
    'content-type': 'application/x-www-form-urlencoded',
    Referer: 'https://www.baidu.com',
    cookie: '__asd=1234'
};
const baseAxios = _axios.default.create({
    httpsAgent: new _https.default.Agent({
        rejectUnauthorized: false
    }),
    responseType: 'arraybuffer',
    headers: baseHeaders
});
const getSource = (timeout)=>{
    const source = _axios.default.CancelToken.source();
    setTimeout(()=>{
        source.cancel();
    }, timeout);
    return source.token;
};
async function postCrawl(url, payload, opts = {}) {
    const { timeout =5000 , headers ={}  } = opts;
    try {
        const result = await baseAxios.post(url, payload, {
            cancelToken: getSource(timeout),
            headers: {
                ...baseHeaders,
                ...headers
            }
        });
        return result.data;
    } catch (error) {
        console.trace(error.message, url, payload);
        throw new Error(`postCrawl failed, ${error.message}`);
    }
}
async function craw(url, opts = {}) {
    const { timeout =5000 , headers ={}  } = opts;
    try {
        const result = await baseAxios.get(url, {
            cancelToken: getSource(timeout),
            headers: {
                ...baseHeaders,
                ...headers
            }
        });
        return result.data;
    } catch (error) {
        console.trace(error.message, url);
        throw new Error(`craw failed, ${error.message}`);
    }
}
async function getCookie(url, opts = {}) {
    const { timeout =5000 , headers ={}  } = opts;
    try {
        var ref;
        const result = await baseAxios.get(url, {
            cancelToken: getSource(timeout),
            headers: {
                ...baseHeaders,
                ...headers
            }
        });
        const setCookie = result.headers['set-cookie'] || [];
        const cookie = (ref = setCookie == null ? void 0 : setCookie.map((item)=>item.split('; ')[0] + ';'
        )) == null ? void 0 : ref.join('');
        return cookie || '';
    } catch (error) {
        console.trace(error, url);
        throw new Error(`get cookie failed, ${error.message}`);
    }
}
async function normalCraw(url, timeout = 5000) {
    for(let i = 0; i <= 3; i++){
        try {
            const { data  } = await _axios.default.get(url, {
                cancelToken: getSource(timeout)
            });
            return data;
        } catch (error) {
            await (0, _).delay(2000);
            console.log(`请求失败，第${i + 1}次重试...`);
        }
    }
    throw new Error('请求失败');
}
